

#include <display_priv.h>

DISPLAY_LayoutCtrl gDISPLAY_layoutCtrl;

int DISPLAY_layoutInit()
{
  int videoDecoderMode = gDISPLAY_ctrl.pCaptureInfo->createPrm.videoDecoderMode;
  
  gDISPLAY_layoutCtrl.displayOffsetH = gDISPLAY_ctrl.frameInfo.lineOffsetH/2; 
  gDISPLAY_layoutCtrl.displayOffsetV = gDISPLAY_ctrl.frameInfo.lineOffsetV;
  gDISPLAY_layoutCtrl.displayWidth   = gDISPLAY_ctrl.frameInfo.width; 
  gDISPLAY_layoutCtrl.displayHeight  = gDISPLAY_ctrl.frameInfo.height;  

  switch(videoDecoderMode)
  {
    case MCVIP_VIDEO_DECODER_MODE_2CH_D1: 
      gDISPLAY_layoutCtrl.numPages = 2;
      gDISPLAY_layoutCtrl.curPage  = 0;

      // PAGE 0
      gDISPLAY_layoutCtrl.page[0].numChannels = 1;
      gDISPLAY_layoutCtrl.page[0].chanNum[0]  = 0;
      gDISPLAY_layoutCtrl.page[0].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[0].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].width;
      gDISPLAY_layoutCtrl.page[0].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].height;              
      gDISPLAY_layoutCtrl.page[0].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetH;
      gDISPLAY_layoutCtrl.page[0].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetV;

      // PAGE 1
      gDISPLAY_layoutCtrl.page[1].numChannels = 1;
      gDISPLAY_layoutCtrl.page[1].chanNum[0]  = 1;
      gDISPLAY_layoutCtrl.page[1].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[1].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[1].width;
      gDISPLAY_layoutCtrl.page[1].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[1].height;              
      gDISPLAY_layoutCtrl.page[1].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[1].offsetH;
      gDISPLAY_layoutCtrl.page[1].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[1].offsetV;

      break;
 
    case MCVIP_VIDEO_DECODER_MODE_4CH_HALF_D1:
      gDISPLAY_layoutCtrl.numPages = 2;
      gDISPLAY_layoutCtrl.curPage  = 0;

      // PAGE 0
      gDISPLAY_layoutCtrl.page[0].numChannels = 2;
      gDISPLAY_layoutCtrl.page[0].chanNum[0]  = 0;
      gDISPLAY_layoutCtrl.page[0].chanNum[1]  = 1;
      gDISPLAY_layoutCtrl.page[0].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[0].fbOffset[1] = 360 * 2;
      gDISPLAY_layoutCtrl.page[0].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].width;
      gDISPLAY_layoutCtrl.page[0].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].height;              
      gDISPLAY_layoutCtrl.page[0].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetH;
      gDISPLAY_layoutCtrl.page[0].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetV;

      // PAGE 1
      gDISPLAY_layoutCtrl.page[1].numChannels = 2;
      gDISPLAY_layoutCtrl.page[1].chanNum[0]  = 2;
      gDISPLAY_layoutCtrl.page[1].chanNum[1]  = 3;
      gDISPLAY_layoutCtrl.page[1].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[1].fbOffset[1] = 360 * 2;
      gDISPLAY_layoutCtrl.page[1].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[2].width;
      gDISPLAY_layoutCtrl.page[1].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[2].height;              
      gDISPLAY_layoutCtrl.page[1].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[2].offsetH;
      gDISPLAY_layoutCtrl.page[1].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[2].offsetV;

      break;   

    case MCVIP_VIDEO_DECODER_MODE_4CH_CIF:
      gDISPLAY_layoutCtrl.numPages = 1;
      gDISPLAY_layoutCtrl.curPage  = 0;

      // PAGE 0
      gDISPLAY_layoutCtrl.page[0].numChannels = 4;
      gDISPLAY_layoutCtrl.page[0].chanNum[0]  = 0;
      gDISPLAY_layoutCtrl.page[0].chanNum[1]  = 1;
      gDISPLAY_layoutCtrl.page[0].chanNum[2]  = 2;
      gDISPLAY_layoutCtrl.page[0].chanNum[3]  = 3;
      gDISPLAY_layoutCtrl.page[0].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[0].fbOffset[1] = 360 * 2;
      gDISPLAY_layoutCtrl.page[0].fbOffset[2] = 720 * (gDISPLAY_layoutCtrl.displayHeight/2) * 2;
      gDISPLAY_layoutCtrl.page[0].fbOffset[3] = (720 * (gDISPLAY_layoutCtrl.displayHeight/2) + 360) * 2;
      gDISPLAY_layoutCtrl.page[0].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].width;
      gDISPLAY_layoutCtrl.page[0].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].height;              
      gDISPLAY_layoutCtrl.page[0].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetH;
      gDISPLAY_layoutCtrl.page[0].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetV;

      break;   
      
    case MCVIP_VIDEO_DECODER_MODE_8CH_CIF:
      gDISPLAY_layoutCtrl.numPages = 2;
      gDISPLAY_layoutCtrl.curPage  = 0;

      // PAGE 0
      gDISPLAY_layoutCtrl.page[0].numChannels = 4;
      gDISPLAY_layoutCtrl.page[0].chanNum[0]  = 0;
      gDISPLAY_layoutCtrl.page[0].chanNum[1]  = 1;
      gDISPLAY_layoutCtrl.page[0].chanNum[2]  = 2;
      gDISPLAY_layoutCtrl.page[0].chanNum[3]  = 3;
      gDISPLAY_layoutCtrl.page[0].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[0].fbOffset[1] = 360 * 2;
      gDISPLAY_layoutCtrl.page[0].fbOffset[2] = 720 * (gDISPLAY_layoutCtrl.displayHeight/2) * 2;
      gDISPLAY_layoutCtrl.page[0].fbOffset[3] = (720 * (gDISPLAY_layoutCtrl.displayHeight/2) + 360) * 2;
      gDISPLAY_layoutCtrl.page[0].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].width;
      gDISPLAY_layoutCtrl.page[0].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].height;              
      gDISPLAY_layoutCtrl.page[0].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetH;
      gDISPLAY_layoutCtrl.page[0].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetV;

      // PAGE 1
      gDISPLAY_layoutCtrl.page[1].numChannels = 4;
      gDISPLAY_layoutCtrl.page[1].chanNum[0]  = 4;
      gDISPLAY_layoutCtrl.page[1].chanNum[1]  = 5;
      gDISPLAY_layoutCtrl.page[1].chanNum[2]  = 6;
      gDISPLAY_layoutCtrl.page[1].chanNum[3]  = 7;
      gDISPLAY_layoutCtrl.page[1].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[1].fbOffset[1] = 360 * 2;
      gDISPLAY_layoutCtrl.page[1].fbOffset[2] = 720 * (gDISPLAY_layoutCtrl.displayHeight/2) * 2;
      gDISPLAY_layoutCtrl.page[1].fbOffset[3] = (720 * (gDISPLAY_layoutCtrl.displayHeight/2) + 360) * 2;
      gDISPLAY_layoutCtrl.page[1].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].width;
      gDISPLAY_layoutCtrl.page[1].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].height;              
      gDISPLAY_layoutCtrl.page[1].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].offsetH;
      gDISPLAY_layoutCtrl.page[1].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].offsetV;

      break;

    case MCVIP_VIDEO_DECODER_MODE_4CH_HALF_D1_PLUS_D1:
      gDISPLAY_layoutCtrl.numPages = 3;
      gDISPLAY_layoutCtrl.curPage  = 0;

      // PAGE 0
      gDISPLAY_layoutCtrl.page[0].numChannels = 2;
      gDISPLAY_layoutCtrl.page[0].chanNum[0]  = 0;
      gDISPLAY_layoutCtrl.page[0].chanNum[1]  = 1;
      gDISPLAY_layoutCtrl.page[0].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[0].fbOffset[1] = 360 * 2;
      gDISPLAY_layoutCtrl.page[0].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].width;
      gDISPLAY_layoutCtrl.page[0].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].height;              
      gDISPLAY_layoutCtrl.page[0].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetH;
      gDISPLAY_layoutCtrl.page[0].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetV;

      // PAGE 1
      gDISPLAY_layoutCtrl.page[1].numChannels = 2;
      gDISPLAY_layoutCtrl.page[1].chanNum[0]  = 2;
      gDISPLAY_layoutCtrl.page[1].chanNum[1]  = 3;
      gDISPLAY_layoutCtrl.page[1].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[1].fbOffset[1] = 360 * 2;
      gDISPLAY_layoutCtrl.page[1].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[2].width;
      gDISPLAY_layoutCtrl.page[1].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[2].height;              
      gDISPLAY_layoutCtrl.page[1].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[2].offsetH;
      gDISPLAY_layoutCtrl.page[1].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[2].offsetV;

      // PAGE 2 
      gDISPLAY_layoutCtrl.page[2].numChannels = 1;
      gDISPLAY_layoutCtrl.page[2].chanNum[0]  = 4;
      gDISPLAY_layoutCtrl.page[2].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[2].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].width;
      gDISPLAY_layoutCtrl.page[2].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].height;              
      gDISPLAY_layoutCtrl.page[2].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].offsetH;
      gDISPLAY_layoutCtrl.page[2].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].offsetV;

      break;      

    case MCVIP_VIDEO_DECODER_MODE_4CH_CIF_PLUS_D1:
      gDISPLAY_layoutCtrl.numPages = 2;
      gDISPLAY_layoutCtrl.curPage  = 0;

      // PAGE 0
      gDISPLAY_layoutCtrl.page[0].numChannels = 4;
      gDISPLAY_layoutCtrl.page[0].chanNum[0]  = 0;
      gDISPLAY_layoutCtrl.page[0].chanNum[1]  = 1;
      gDISPLAY_layoutCtrl.page[0].chanNum[2]  = 2;
      gDISPLAY_layoutCtrl.page[0].chanNum[3]  = 3;
      gDISPLAY_layoutCtrl.page[0].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[0].fbOffset[1] = 360 * 2;
      gDISPLAY_layoutCtrl.page[0].fbOffset[2] = 720 * (gDISPLAY_layoutCtrl.displayHeight/2) * 2;
      gDISPLAY_layoutCtrl.page[0].fbOffset[3] = (720 * (gDISPLAY_layoutCtrl.displayHeight/2) + 360) * 2;
      gDISPLAY_layoutCtrl.page[0].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].width;
      gDISPLAY_layoutCtrl.page[0].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].height;              
      gDISPLAY_layoutCtrl.page[0].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetH;
      gDISPLAY_layoutCtrl.page[0].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetV;

      // PAGE 1 
      gDISPLAY_layoutCtrl.page[1].numChannels = 1;
      gDISPLAY_layoutCtrl.page[1].chanNum[0]  = 4;
      gDISPLAY_layoutCtrl.page[1].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[1].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].width;
      gDISPLAY_layoutCtrl.page[1].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].height;              
      gDISPLAY_layoutCtrl.page[1].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].offsetH;
      gDISPLAY_layoutCtrl.page[1].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].offsetV;

      break;   

    case MCVIP_VIDEO_DECODER_MODE_8CH_CIF_PLUS_D1:
      gDISPLAY_layoutCtrl.numPages = 3;
      gDISPLAY_layoutCtrl.curPage  = 0;

      // PAGE 0
      gDISPLAY_layoutCtrl.page[0].numChannels = 4;
      gDISPLAY_layoutCtrl.page[0].chanNum[0]  = 0;
      gDISPLAY_layoutCtrl.page[0].chanNum[1]  = 1;
      gDISPLAY_layoutCtrl.page[0].chanNum[2]  = 2;
      gDISPLAY_layoutCtrl.page[0].chanNum[3]  = 3;
      gDISPLAY_layoutCtrl.page[0].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[0].fbOffset[1] = 360 * 2;
      gDISPLAY_layoutCtrl.page[0].fbOffset[2] = 720 * (gDISPLAY_layoutCtrl.displayHeight/2) * 2;
      gDISPLAY_layoutCtrl.page[0].fbOffset[3] = (720 * (gDISPLAY_layoutCtrl.displayHeight/2) + 360) * 2;
      gDISPLAY_layoutCtrl.page[0].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].width;
      gDISPLAY_layoutCtrl.page[0].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].height;              
      gDISPLAY_layoutCtrl.page[0].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetH;
      gDISPLAY_layoutCtrl.page[0].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[0].offsetV;

      // PAGE 1
      gDISPLAY_layoutCtrl.page[1].numChannels = 4;
      gDISPLAY_layoutCtrl.page[1].chanNum[0]  = 4;
      gDISPLAY_layoutCtrl.page[1].chanNum[1]  = 5;
      gDISPLAY_layoutCtrl.page[1].chanNum[2]  = 6;
      gDISPLAY_layoutCtrl.page[1].chanNum[3]  = 7;
      gDISPLAY_layoutCtrl.page[1].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[1].fbOffset[1] = 360 * 2;
      gDISPLAY_layoutCtrl.page[1].fbOffset[2] = 720 * (gDISPLAY_layoutCtrl.displayHeight/2) * 2;
      gDISPLAY_layoutCtrl.page[1].fbOffset[3] = (720 * (gDISPLAY_layoutCtrl.displayHeight/2) + 360) * 2;
      gDISPLAY_layoutCtrl.page[1].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].width;
      gDISPLAY_layoutCtrl.page[1].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].height;              
      gDISPLAY_layoutCtrl.page[1].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].offsetH;
      gDISPLAY_layoutCtrl.page[1].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[4].offsetV;

      // PAGE 2 
      gDISPLAY_layoutCtrl.page[2].numChannels = 1;
      gDISPLAY_layoutCtrl.page[2].chanNum[0]  = 8;
      gDISPLAY_layoutCtrl.page[2].fbOffset[0] = 0;
      gDISPLAY_layoutCtrl.page[2].width       = gDISPLAY_ctrl.pCaptureInfo->chList.info[8].width;
      gDISPLAY_layoutCtrl.page[2].height      = gDISPLAY_ctrl.pCaptureInfo->chList.info[8].height;              
      gDISPLAY_layoutCtrl.page[2].offsetH     = gDISPLAY_ctrl.pCaptureInfo->chList.info[8].offsetH;
      gDISPLAY_layoutCtrl.page[2].offsetV     = gDISPLAY_ctrl.pCaptureInfo->chList.info[8].offsetV;

      break;     

    default:
      return OSA_EFAIL; 
  }

  DISPLAY_layoutPrintCurPageProperties();
        
  return OSA_SOK;
}


int DISPLAY_layoutRun(DISPLAY_BufInfo *buf,Bool nextPage)
{
  int i, bufId,curPage,chNum;
  MCVIP_DmaPrm dmaPrm;
  MCVIP_BufInfo *pCaptureBufInfo;
  //static int count = 0;

  if(nextPage == TRUE)
  {
    // Goto next page
    gDISPLAY_layoutCtrl.curPage ++;

    if((gDISPLAY_layoutCtrl.curPage % gDISPLAY_layoutCtrl.numPages) == 0)
    {
      gDISPLAY_layoutCtrl.curPage = 0;  
    }    

    DISPLAY_layoutPrintCurPageProperties();   
  }

  curPage = gDISPLAY_layoutCtrl.curPage;

  dmaPrm.copyWidth  = gDISPLAY_layoutCtrl.page[curPage].width;
  dmaPrm.copyHeight = gDISPLAY_layoutCtrl.page[curPage].height;

  dmaPrm.srcOffsetH = gDISPLAY_layoutCtrl.page[curPage].offsetH;
  dmaPrm.srcOffsetV = gDISPLAY_layoutCtrl.page[curPage].offsetV;

  dmaPrm.dstOffsetH = gDISPLAY_layoutCtrl.displayOffsetH;
  dmaPrm.dstOffsetV = gDISPLAY_layoutCtrl.displayOffsetV;
  
  dmaPrm.fillValueY  = DISPLAY_COLOR_NO_VIDEO_Y;
  dmaPrm.fillValueC  = DISPLAY_COLOR_NO_VIDEO_C;    

  for(i = 0;i < gDISPLAY_layoutCtrl.page[curPage].numChannels;i ++)
  {
    dmaPrm.dstPhysAddr = NULL; // if NULL, the virtual to physical done inside DMA API  
    dmaPrm.dstVirtAddr = buf->virtAddr + gDISPLAY_layoutCtrl.page[curPage].fbOffset[i];  

    chNum = gDISPLAY_layoutCtrl.page[curPage].chanNum[i];
    bufId = gDISPLAY_ctrl.chInfo[chNum].curBufId;

    dmaPrm.srcPhysAddr = NULL; // fill dst with 'blank' data
    dmaPrm.srcVirtAddr = NULL; // fill dst with 'blank' data
      
    pCaptureBufInfo = MCVIP_getBufInfo(gDISPLAY_ctrl.pCaptureInfo->mcvipHndl, bufId);
    
    if(pCaptureBufInfo != NULL) 
    {
      dmaPrm.srcPhysAddr = pCaptureBufInfo->physAddr;
      dmaPrm.srcVirtAddr = pCaptureBufInfo->virtAddr;
    }

    MCVIP_dmaRun(&dmaPrm);
  }
  
  return OSA_SOK;
}
int DISPLAY_layoutPrintCurPageProperties()
{
  int curPage,chanNum,i;

  curPage = gDISPLAY_layoutCtrl.curPage;

  printf("\n\n");
  printf(" PAGE %d PROPERTIES\n",curPage);
  printf(" ------------------\n");
  printf(" Num of Channels = %d\n\n",gDISPLAY_layoutCtrl.page[curPage].numChannels);
  for(i = 0;i < gDISPLAY_layoutCtrl.page[curPage].numChannels;i ++)
  { 
    chanNum = gDISPLAY_layoutCtrl.page[curPage].chanNum[i]; 
    printf(" Channel No = %d   :Width = %d ,Height = %d\n",chanNum,
                                                           gDISPLAY_layoutCtrl.page[curPage].width,
                                                           gDISPLAY_layoutCtrl.page[curPage].height);
  }

  printf("\n\n");

  return OSA_SOK;
}
